﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.SparseTexture
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Runtime.CompilerServices;
using UnityEngine.Bindings;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Internal;

#nullable disable
namespace UnityEngine
{
  /// <summary>
  ///   <para>Class for handling Sparse Textures.</para>
  /// </summary>
  [NativeHeader("Runtime/Graphics/SparseTexture.h")]
  public sealed class SparseTexture : Texture
  {
    /// <summary>
    ///   <para>Get sparse texture tile width (Read Only).</para>
    /// </summary>
    public extern int tileWidth { [MethodImpl(MethodImplOptions.InternalCall)] get; }

    /// <summary>
    ///   <para>Get sparse texture tile height (Read Only).</para>
    /// </summary>
    public extern int tileHeight { [MethodImpl(MethodImplOptions.InternalCall)] get; }

    /// <summary>
    ///   <para>Is the sparse texture actually created? (Read Only)</para>
    /// </summary>
    public extern bool isCreated { [NativeName("IsInitialized"), MethodImpl(MethodImplOptions.InternalCall)] get; }

    [FreeFunction(Name = "SparseTextureScripting::Create", ThrowsException = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void Internal_Create(
      [Writable] SparseTexture mono,
      int width,
      int height,
      GraphicsFormat format,
      TextureColorSpace colorSpace,
      int mipCount);

    /// <summary>
    ///   <para>Update sparse texture tile with color values.</para>
    /// </summary>
    /// <param name="tileX">Tile X coordinate.</param>
    /// <param name="tileY">Tile Y coordinate.</param>
    /// <param name="miplevel">Mipmap level of the texture.</param>
    /// <param name="data">Tile color data.</param>
    [FreeFunction(Name = "SparseTextureScripting::UpdateTile", HasExplicitThis = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public extern void UpdateTile(int tileX, int tileY, int miplevel, [Unmarshalled] Color32[] data);

    /// <summary>
    ///   <para>Update sparse texture tile with raw pixel values.</para>
    /// </summary>
    /// <param name="tileX">Tile X coordinate.</param>
    /// <param name="tileY">Tile Y coordinate.</param>
    /// <param name="miplevel">Mipmap level of the texture.</param>
    /// <param name="data">Tile raw pixel data.</param>
    [FreeFunction(Name = "SparseTextureScripting::UpdateTileRaw", HasExplicitThis = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public extern void UpdateTileRaw(int tileX, int tileY, int miplevel, [Unmarshalled] byte[] data);

    /// <summary>
    ///   <para>Unload sparse texture tile.</para>
    /// </summary>
    /// <param name="tileX">Tile X coordinate.</param>
    /// <param name="tileY">Tile Y coordinate.</param>
    /// <param name="miplevel">Mipmap level of the texture.</param>
    public void UnloadTile(int tileX, int tileY, int miplevel)
    {
      this.UpdateTileRaw(tileX, tileY, miplevel, (byte[]) null);
    }

    internal bool ValidateFormat(TextureFormat format, int width, int height)
    {
      bool flag = this.ValidateFormat(format);
      if (flag && TextureFormat.PVRTC_RGB2 <= format && format <= TextureFormat.PVRTC_RGBA4 && (width != height || !Mathf.IsPowerOfTwo(width)))
        throw new UnityException(string.Format("'{0}' demands texture to be square and have power-of-two dimensions", (object) format.ToString()));
      return flag;
    }

    internal bool ValidateFormat(GraphicsFormat format, int width, int height)
    {
      bool flag = this.ValidateFormat(format, FormatUsage.Sparse);
      if (flag && GraphicsFormatUtility.IsPVRTCFormat(format) && (width != height || !Mathf.IsPowerOfTwo(width)))
        throw new UnityException(string.Format("'{0}' demands texture to be square and have power-of-two dimensions", (object) format.ToString()));
      return flag;
    }

    internal bool ValidateSize(int width, int height, GraphicsFormat format)
    {
      if ((long) GraphicsFormatUtility.GetBlockSize(format) * ((long) width / (long) GraphicsFormatUtility.GetBlockWidth(format)) * ((long) height / (long) GraphicsFormatUtility.GetBlockHeight(format)) >= 65536L)
        return true;
      Debug.LogError((object) "SparseTexture creation failed. The minimum size in bytes of a SparseTexture is 64KB.", (Object) this);
      return false;
    }

    private static void ValidateIsNotCrunched(TextureFormat textureFormat)
    {
      if (GraphicsFormatUtility.IsCrunchFormat(textureFormat))
        throw new ArgumentException("Crunched SparseTexture is not supported.");
    }

    [ExcludeFromDocs]
    public SparseTexture(int width, int height, DefaultFormat format, int mipCount)
      : this(width, height, SystemInfo.GetGraphicsFormat(format), mipCount)
    {
    }

    [ExcludeFromDocs]
    public SparseTexture(int width, int height, GraphicsFormat format, int mipCount)
    {
      if (!this.ValidateFormat(format, width, height) || !this.ValidateSize(width, height, format))
        return;
      SparseTexture.Internal_Create(this, width, height, format, this.GetTextureColorSpace(format), mipCount);
    }

    /// <summary>
    ///   <para>Create a sparse texture.</para>
    /// </summary>
    /// <param name="width">Texture width in pixels.</param>
    /// <param name="height">Texture height in pixels.</param>
    /// <param name="mipCount">Mipmap count. Pass -1 to create full mipmap chain.</param>
    /// <param name="linear">Whether texture data will be in linear or sRGB color space (default is sRGB).</param>
    /// <param name="textureFormat">Texture Format.</param>
    [ExcludeFromDocs]
    public SparseTexture(int width, int height, TextureFormat textureFormat, int mipCount)
      : this(width, height, textureFormat, mipCount, false)
    {
    }

    /// <summary>
    ///   <para>Create a sparse texture.</para>
    /// </summary>
    /// <param name="width">Texture width in pixels.</param>
    /// <param name="height">Texture height in pixels.</param>
    /// <param name="mipCount">Mipmap count. Pass -1 to create full mipmap chain.</param>
    /// <param name="linear">Whether texture data will be in linear or sRGB color space (default is sRGB).</param>
    /// <param name="textureFormat">Texture Format.</param>
    public SparseTexture(
      int width,
      int height,
      TextureFormat textureFormat,
      int mipCount,
      [DefaultValue("false")] bool linear)
    {
      if (!this.ValidateFormat(textureFormat, width, height))
        return;
      SparseTexture.ValidateIsNotCrunched(textureFormat);
      GraphicsFormat graphicsFormat = GraphicsFormatUtility.GetGraphicsFormat(textureFormat, !linear);
      if (!SystemInfo.IsFormatSupported(graphicsFormat, FormatUsage.Sparse))
      {
        Debug.LogError((object) string.Format("Creation of a SparseTexture with '{0}' is not supported on this platform.", (object) textureFormat));
      }
      else
      {
        if (!this.ValidateSize(width, height, graphicsFormat))
          return;
        SparseTexture.Internal_Create(this, width, height, graphicsFormat, this.GetTextureColorSpace(linear), mipCount);
      }
    }
  }
}
